package at.fhv.components.dilate;

import java.awt.BorderLayout;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.beans.Customizer;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;

import javax.media.jai.KernelJAI;
import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JPanel;

import at.fhv.framework.customizerTools.MatrixTablePanel;

/**
 * The DilateCustomizer class.
 * 
 * @author Michael Sieber
 */
public class DilateCustomizer extends JPanel implements Customizer, ActionListener
{
	private static final long		serialVersionUID	= -5576304645898860859L;
	private PropertyChangeSupport	_pcsupport			= new PropertyChangeSupport(this);
	private MatrixTablePanel		_matrixTablePanel;
	private JButton					_apply;
	private DilateFilter			_dilateFilter;

	public DilateCustomizer()
	{
		initGUI();
	}

	/*
	 * {@inheritDoc}
	 */
	@Override
	public void setObject(Object obj) {
		_dilateFilter = (DilateFilter) obj;
		KernelJAI kernel = _dilateFilter.getKernel();
		_matrixTablePanel.setTableData(kernel.getKernelData(), kernel.getHeight(), kernel.getWidth());
	}

	@Override
	public void addPropertyChangeListener(PropertyChangeListener pcl) {
		_pcsupport.addPropertyChangeListener(pcl);
	}

	@Override
	public void removePropertyChangeListener(PropertyChangeListener pcl) {
		_pcsupport.removePropertyChangeListener(pcl);
	}

	/*
	 * {@inheritDoc}
	 */
	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource().equals(_apply))
		{
			_dilateFilter.setKernel(new KernelJAI(_matrixTablePanel.getKernelHeight(),
					_matrixTablePanel.getKernelWidth(), _matrixTablePanel.translateKernel()));
		}
	}

	/**
	 * Initialize the gui
	 */
	private void initGUI()
	{
		_matrixTablePanel = new MatrixTablePanel();

		setLayout(new BorderLayout());

		_apply = new JButton("apply");
		_apply.addActionListener(this);

		JPanel westPanel = new JPanel(new FlowLayout(FlowLayout.LEFT));
		westPanel.add(new JLabel("dilate matrix"));

		JPanel centerPanel = new JPanel(new FlowLayout(FlowLayout.CENTER));
		centerPanel.add(_matrixTablePanel);

		JPanel southPanel = new JPanel(new FlowLayout(FlowLayout.RIGHT));
		southPanel.add(_apply);
		
		add(westPanel, BorderLayout.WEST);
		add(centerPanel, BorderLayout.CENTER);
		add(southPanel, BorderLayout.SOUTH);
	}
}